package com.cloudkinto.service.consumer;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSONObject;
import com.cloudkinto.bo.wave.WaveForceFinishBo;
import com.cloudkinto.common.RabbitQueueConstant;
import com.cloudkinto.entity.OrderInputDo;
import com.cloudkinto.entity.OrderOutboundDo;
import com.cloudkinto.entity.OutboundPickingDo;
import com.cloudkinto.service.event.order.OutboundAddCostEvent;
import com.cloudkinto.service.event.order.OutboundAutoSendStatusEvent;
import com.cloudkinto.service.event.order.OutboundPostOperationEvent;
import com.cloudkinto.service.order.outbound.OutboundImportExportService;
import com.cloudkinto.service.order.outbound.OutboundService;
import com.cloudkinto.service.order.outbound.OutboundWaveService;
import com.cloudkinto.service.order.outbound.vo.OrderOutboundImportTrackNumberReq;
import com.cloudkinto.service.order.shelf.OrderInputShelfService;
import com.cloudkinto.service.wr.QuotationSchemeService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * @author long
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class OrderConsumer {

    private final QuotationSchemeService quotationSchemeService;
    private final OutboundService outboundService;
    private final OrderInputShelfService shelfService;
    private final OutboundImportExportService outboundImportExportService;
    private final OutboundWaveService waveService;

//    /**
//     * 出库单预估成本
//     *
//     * @param msg
//     */
//    @RabbitListener(queuesToDeclare = @Queue(RabbitQueueConstant.Outbound_Estimate_Cost))
//    public void outboundEstimateCost(String msg) {
//        quotationSchemeService.costAddOut(outboundService.buildOutboundCostReq(JSONObject.parseObject(msg, OrderOutboundDo.class), 1));
//    }

//    /**
//     * 出库单实扣
//     *
//     * @param msg
//     */
//    @RabbitListener(queuesToDeclare = @Queue(RabbitQueueConstant.Outbound_Real_Cost))
//    public void outboundRealCost(String msg) {
//        quotationSchemeService.costAddOut(outboundService.buildOutboundCostReq(JSONObject.parseObject(msg, OrderOutboundDo.class), 0));
//    }

    /**
     * 上架预扣
     *
     * @param msg
     */
    @RabbitListener(queuesToDeclare = @Queue(RabbitQueueConstant.Shelf_Estimate_Cost))
    public void shelfEstimateCost(String msg) {
        quotationSchemeService.costAddInput(shelfService.buildInboundCostReq(JSONObject.parseObject(msg, OrderInputDo.class), 1));
    }

    /**
     * 上架实扣
     *
     * @param msg
     */
    @RabbitListener(queuesToDeclare = @Queue(RabbitQueueConstant.Shelf_Real_Cost))
    public void shelfRealCost(String msg) {
        quotationSchemeService.costAddInput(shelfService.buildInboundCostReq(JSONObject.parseObject(msg, OrderInputDo.class), 0));
    }

    @RabbitListener(queuesToDeclare = @Queue(RabbitQueueConstant.Outbound_Auto_Send_Log))
    public void outboundAutoSendLog(String msg) {
        if (StrUtil.isBlank(msg)) {
            return;
        }
        OutboundAutoSendStatusEvent event = JSONUtil.toBean(msg, OutboundAutoSendStatusEvent.class);
        if (CollUtil.isNotEmpty(event.getOutboundDoList())) {
            outboundService.autoSendOutboundHandler(event.getOutboundDoList(), event.getUserId(), event.getUsername());
        }
    }

    @RabbitListener(queuesToDeclare = @Queue(RabbitQueueConstant.Outbound_Add_Cost))
    public void outboundAddCost(String msg) {
        if (StrUtil.isBlank(msg)) {
            return;
        }
        try {
            OutboundAddCostEvent event = JSONUtil.toBean(msg, OutboundAddCostEvent.class);
            if (CollUtil.isNotEmpty(event.getOutboundDoList())) {
                for (OrderOutboundDo orderOutboundDo : event.getOutboundDoList()) {
                    quotationSchemeService.costAddOut(outboundService.buildOutboundCostReq(orderOutboundDo, event.getStatus()));
                }
            } else {
                quotationSchemeService.costAddOut(outboundService.buildOutboundCostReq(event.getOutboundDo(), event.getStatus()));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @RabbitListener(queuesToDeclare = @Queue(RabbitQueueConstant.Outbound_Post_Operation))
    public void outboundPostOperation(String msg) {
        if (StrUtil.isBlank(msg)) {
            return;
        }
        OutboundPostOperationEvent event = JSONUtil.toBean(msg, OutboundPostOperationEvent.class);
        Map<OrderOutboundDo, List<OutboundPickingDo>> postHandleMap = event.getPostHandleMap();
        if (CollUtil.isNotEmpty(postHandleMap)) {
            for (Map.Entry<OrderOutboundDo, List<OutboundPickingDo>> entry : postHandleMap.entrySet()) {
                outboundService.postOutboundOperation(entry.getKey(), entry.getValue());
            }
        }
    }


    @RabbitListener(queuesToDeclare = @Queue(RabbitQueueConstant.Order_Outbound_ImportTrackNumber))
    public void outboundImportTrackNumber(String msg) {
        if (StrUtil.isBlank(msg)) {
            return;
        }
        try {
            OrderOutboundImportTrackNumberReq req = JSONObject.parseObject(msg, OrderOutboundImportTrackNumberReq.class);
            outboundImportExportService.dealImportUpdateTrackingNumber(req.getImportDtoList(), req.getOrderType(), req.getTenantId(), req.getWhId(), req.getUserId(), req.getUsername(), req.getFrom());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @RabbitListener(queuesToDeclare = @Queue(RabbitQueueConstant.Wave_Force_Finish))
    public void waveForceFinish(String msg) {
        if (StrUtil.isBlank(msg)) {
            return;
        }
        try {
            WaveForceFinishBo waveForceFinishBo = JSONUtil.toBean(msg, WaveForceFinishBo.class);
            waveService.waveForceFinish(waveForceFinishBo.getWaveId(), waveForceFinishBo.getUserId(), waveForceFinishBo.getUsername());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
